package views;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.zx.theline.R;

import GsonBean.ZsrjhBean;
import Utils.DensityUtil;

/**
 * Created by Ren on 2017/7/27.
 */

public class ZsrjhView extends RelativeLayout {

    LinearLayout view;
    FrameLayout tablelayout;

    ZsrjhBean.RowsBean mBean;

    //是否已经是多选布局
    int isGroupDoneLayout = 0;

    int position;

    //日计划号
    TextView rjhh;
    //施工日期
    TextView sgrq;
    //类别
    TextView lb;
    //线名
    TextView xm;
    //行别
    TextView hb;
    //施工地点
    TextView sgdd;
    //施工项目
    TextView sgxm;
    //施工内容
    TextView sgnr;
    //施工机械
    TextView sgjx;
    //B类防护
    TextView blfh;
    //施工单位
    TextView sgdw;
    //状态
    TextView zt;
    //操作按钮图片
    TextView mImageView;

    LinearLayout zt_layout;
    //B类防御layout
    LinearLayout layout_blfh;

    //批量操作 是否选中
    CheckBox mCheckBox;

    public ZsrjhView(Context context, boolean isGroupDone) {
        super(context);
        initView();
        initDate();
        initListen();
    }

    public ZsrjhView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
        initDate();
        initListen();
    }

    public ZsrjhView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        initDate();
        initListen();
    }


    private void initView() {


        view = (LinearLayout) inflate(getContext(), R.layout.zsrjhview, null);
        addView(view);

        rjhh = (TextView) findViewById(R.id.rjhh);
        sgrq = (TextView) findViewById(R.id.sgrq);
        lb = (TextView) findViewById(R.id.lb);
        xm = (TextView) findViewById(R.id.xm);
        hb = (TextView) findViewById(R.id.hb);
        sgdd = (TextView) findViewById(R.id.sgdd);
        sgxm = (TextView) findViewById(R.id.sgxm);
        sgnr = (TextView) findViewById(R.id.sgnr);
        sgjx = (TextView) findViewById(R.id.sgjx);
        blfh = (TextView) findViewById(R.id.blfh);
        sgdw = (TextView) findViewById(R.id.sgdw);
        mImageView = (TextView) findViewById(R.id.mImageView);
        mCheckBox = (CheckBox) findViewById(R.id.checkbox);
        tablelayout = (FrameLayout) findViewById(R.id.tablelayout);
        zt_layout = (LinearLayout) findViewById(R.id.zt_layout);
    }

    private void initDate() {

    }

    private void initListen() {

        mImageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });


    }

    //进行批量操作
    public void toGroupDone() {

        if (isGroupDoneLayout == 0) {
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mCheckBox.isChecked()) {
                        mCheckBox.setChecked(false);
                    } else {
                        mCheckBox.setChecked(true);
                    }
                }
            });

            //动画改变数据区域宽度，给checkbox空出位置

            ValueAnimator animator = ValueAnimator.ofFloat(0, (DensityUtil.dip2px(getContext(), 40)));
            animator.setTarget(tablelayout);
            animator.setDuration(200).start();

            //监听动画 改变表格宽度
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    RelativeLayout.LayoutParams lp;
                    lp = (LayoutParams) tablelayout.getLayoutParams();
                    lp.leftMargin = ((Float) animation.getAnimatedValue()).intValue();
                    tablelayout.setLayoutParams(lp);
                }
            });
            //动画结束时，显示checkbox
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    mCheckBox.setVisibility(VISIBLE);

                }
            });
            isGroupDoneLayout++;
        }
    }

    public void cancelGroupDone() {
        if (isGroupDoneLayout > 0) {
            view.setOnClickListener(null);
            ValueAnimator animator = ValueAnimator.ofFloat(40, 0);
            animator.setTarget(tablelayout);
            animator.setDuration(200).start();
            mCheckBox.setVisibility(GONE);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    RelativeLayout.LayoutParams lp;
                    lp = (LayoutParams) tablelayout.getLayoutParams();
                    lp.leftMargin = ((Float) animation.getAnimatedValue()).intValue();
                    tablelayout.setLayoutParams(lp);
                }
            });
            isGroupDoneLayout = 0;
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        setMeasuredDimension(widthSize, getMeasuredHeight());
    }


    public void setDate(ZsrjhBean.RowsBean mBean) {
        this.mBean = mBean;
        rjhh.setText(mBean.getBh());
        sgrq.setText(mBean.getExsgrq());
        lb.setText(mBean.getLb() + "类");
        if (mBean.getLb().equals("B")) {
            lb.setTextColor(getResources().getColor(R.color.colorTextB));
        } else if (mBean.getLb().equals("C")) {
            lb.setTextColor(getResources().getColor(R.color.colorTextC));
        }
        xm.setText(mBean.getXlmc());
        hb.setText(mBean.getXbmc());
        sgdd.setText(mBean.getSgdd());
        sgxm.setText(mBean.getXmmc());
        sgnr.setText(mBean.getNrmc());
        sgjx.setText(mBean.getJxmc());
        blfh.setText(mBean.getBlfhmc());
        sgdw.setText(mBean.getSgdwmc());

    }


    public void hideZt() {
        zt_layout.setVisibility(GONE);
    }


}

